package com.exsun.ex1077.ui.activity.feedback

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.exsun.ex1077.network.apiService
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.extend.toFileFormPart
import com.zhangping.fastjetpack.extend.toJson

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/11 10:20
 */
class FeedbackViewModel: BaseViewModel() {

    val content = StringObservableField()
    val tel = StringObservableField()
    val submitEnable = BooleanObservableField(true)

    val submitResult = MutableLiveData<ResultState<Any?>>()

    fun uploadPhoto(pathList: List<String>): MutableLiveData<List<String>> {
        val liveData = MutableLiveData<List<String>>()
        val pair = pathList.map { it.toFileFormPart() }
        request({ apiService.photoUpload(pair)}, {
            liveData.value = it
        }, {}, true)
        return liveData
    }

    fun submitFeedback(photoList: List<String>?) {
        request({ apiService.feedbackSubmit(JSONObject().apply {
            put("feedbackDetails", content.get())
            put("phoneNumber", tel.get())
            photoList?.let {
                put("imageList", it.map { JSONObject().apply { put("path", it) } })
            }
        })}, submitResult)
    }
}